package com.pz.cloud.framework.leagueMem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMemPunishment;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemPunishmentService;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 团员处分
 * @Author: jeecg-boot
 * @Date: 2020-12-05
 * @Version: V1.0
 */
@Api(tags = "团员处分")
@RestController
@RequestMapping("/leagueMemPunish")
@Slf4j
public class LeagueMemPunishmentController extends BaseController<LeagueMemPunishment, ILeagueMemPunishmentService> {
    @Autowired
    private ILeagueMemPunishmentService leagueMemPunishmentService;

    @Autowired
    private ILeagueMemService leagueMemService;

    /**
     * 分页列表查询
     *
     * @param leagueMemPunishment
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "团员处分-分页列表查询")
    @ApiOperation(value = "团员处分-分页列表查询", notes = "团员处分-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent = "league/LeagueMemPunishmentList")
    public Result<?> queryPageList(LeagueMemPunishment leagueMemPunishment,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        IPage<LeagueMemPunishment> pageList = leagueMemPunishmentService.findPage(leagueMemPunishment, req.getParameterMap());
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param leagueMemPunishment
     * @return
     */
    @AutoLog(value = "团员处分-添加")
    @ApiOperation(value = "团员处分-添加", notes = "团员处分-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LeagueMemPunishment leagueMemPunishment) {
        String memId = leagueMemPunishment.getMemId();
        if (StringUtils.isEmpty(memId)) {
            String userId = leagueMemPunishment.getUserId();
            if (StringUtils.isEmpty(userId)) {
                return Result.error("用户id不能为空");
            }
            LeagueMem leagueMem = leagueMemService.getByUserId(userId);
            if (leagueMem == null) {
                return Result.error("该团员不存在");
            }
            leagueMemPunishment.setMemId(leagueMem.getId());
        }
        leagueMemPunishmentService.save(leagueMemPunishment);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param leagueMemPunishment
     * @return
     */
    @AutoLog(value = "团员处分-编辑")
    @ApiOperation(value = "团员处分-编辑", notes = "团员处分-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LeagueMemPunishment leagueMemPunishment) {
        leagueMemPunishmentService.updateById(leagueMemPunishment);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "团员处分-通过id删除")
    @ApiOperation(value = "团员处分-通过id删除", notes = "团员处分-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        leagueMemPunishmentService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "团员处分-批量删除")
    @ApiOperation(value = "团员处分-批量删除", notes = "团员处分-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.leagueMemPunishmentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "团员处分-通过id查询")
    @ApiOperation(value = "团员处分-通过id查询", notes = "团员处分-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LeagueMemPunishment leagueMemPunishment = leagueMemPunishmentService.getById(id);
        if (leagueMemPunishment == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(leagueMemPunishment);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param leagueMemPunishment
     */
    @RequestMapping(value = "/exportXls")
    @PermissionData(pageComponent = "league/LeagueMemPunishmentList")
    public ModelAndView exportXls(HttpServletRequest request, LeagueMemPunishment leagueMemPunishment) {
        List<LeagueMemPunishment> list = leagueMemPunishmentService.list(leagueMemPunishment, request.getParameterMap());
        return super.exportXls(request, list, LeagueMemPunishment.class, "团员处分");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LeagueMemPunishment.class);
    }

}
